package StackPractice;

import java.util.Stack;

public class MinStack {
    Stack<Integer> stackData = new Stack<>();
    Stack<Integer> stackMin = new Stack<>();

    public void push(int value) {
        if (stackData.isEmpty() || stackMin.peek() >= value) {
            stackMin.push(value);
        }
        stackData.push(value);
    }

    public void pop() {
        if (!stackData.isEmpty()) {
            if (stackData.peek().intValue() == stackMin.peek().intValue()) {
                stackData.pop();
                stackMin.pop();
                return;
            }
            stackData.pop();
        }
    }

    public int top() {
        return stackData.peek();
    }

    public int getMin() {
        return stackMin.peek();
    }

    @Override
    public String toString() {
        return "MinStack{" +
                "stackData=" + stackData +
                ", stackMin=" + stackMin +
                '}';
    }

    public static void main(String[] args) {
        MinStack minStack = new MinStack();

        minStack.push(512);
        minStack.push(-1024);
        minStack.push(-1024);
        minStack.push(512);
        minStack.pop();
        int min = minStack.getMin();
        System.out.println(min);
        minStack.pop();
        int min1 = minStack.getMin();
        System.out.println(min1);
        minStack.pop();
        int min2 = minStack.getMin();
        System.out.println(min2);
        System.out.println(minStack.toString());
    }
}
